<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Form
 *
 * @author Jira
 */
class Form {

    private $legend;

     function __construct(array $link,$legend=null,$upload=false) {
        $html = html::getInstance();
        $link = $html->makeUrl($link);
        $this->legend = $legend;
        $form = null;
        if($legend != null)
            $form .= '<fieldset><legend>'.$legend.'</legend>';
        $form .= '<form action="'.$link.'" method="post"';
        if($upload)
            $form .= 'enctype="multipart/form-data"';
        $form .= '>';
        echo $form;
     }

    public function input($type,$name,$text = null ,$nobr = false) {
        $input = null;
        if(!is_null($text))
            $input .= '<label form="'.$name.'">'.$text.'</label>';
        $input .= '<input type="'.$type.'" name="'.$name.'"';
        if(!is_null($text))
            $input .= 'id="'.$name.'"';
        $input .= '/>';
        if(!$nobr)
            $input .= '<br />';
        return $input;
    }

    public function  endForm($text = null) {
        $endform = null;
        if(!is_null($text)) {
            $endform = '<input type="submit" name="submit" value="'.$text.'" />';
        }
        $endform .= '</form>';
        if($this->legend != null)
            $endform .= '</fieldset>';
        return $endform;
    }

    public function group($legend = null) {
        $group  = '<fieldset>';
        if(!is_null($legend))
            $group .= '<legend>'.$legend.'</legend>';
        return $group;
    }

    public function endgroup() {
        return '</fieldset>';
    }

    public function label($text) {
        return '<label>'.$text.'</label>';
    }

    public function days($name,$br = false) {
        $days = '<select name="'.$name.'">';
        for($i = 1; $i <= 31; $i++)
            $days .= $this->addOptions ($i, $i);
        $days .= '</select>';
        if($br)
            $days .= '<br />';
        return $days;
    }

    public function months($name,$br=false) {
        $date = date::getInstance();
        $months = '<select name="'.$name.'">';
        for($i = 1; $i <= 12;$i++)
            $months .= $this->addOptions($i, $date->getMonth($i));
        $months .= '</select>';
        if($br)
            $months .= '<br />';
        return $months;
    }

    public function years($name,$br=false) {
        $years = '<select name="'.$name.'">';
        for($i = (date('Y')-70);$i<=date('Y')-10;$i++)
            $years .= $this->addOptions($i,$i);
        $years .= '</select>';
        if($br)
            $years .= '<br />';
        return $years;
    }

    private function addOptions($value,$text) {
        if(is_numeric($text) && $text<10)
            $text = '0'.$text;
        return '<option value="'.$value.'">'.$text.'</option>';
    }
    
}
?>
